/**
 * HolderAdapter 通用缓存适配器
 */
package com.nest.WorkNest.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * name HolderAdapter
 * 
 * @author:suligen
 * 
 */
public class EditTextAdapter<E> extends BaseAdapter {

	public interface ViewCreator<E> {
		/**
		 * title:创建View description:创建View,HolderAdapter需要创建View时，会调用此方法创建View。
		 * 
		 * @param inflater
		 * @param position
		 * @param data
		 * @return view
		 */
		View createView(LayoutInflater inflater, int position, E data);

		/**
		 * title:更新View description :更新View
		 * 
		 * @param view
		 * @param position
		 * @param data
		 */
		void updateView(View view, int position, E map);
	};

	/**
	 * name:ViewHolder description:一个持有View引用对象的静态类，用以减少View的创建次数
	 *
	 */
	private static class ViewHolder {
		public View view;
	}

	/**
	 * 数据缓存
	 */
	private List<E> mDataCache;

	private int position;

	/**
	 * 用于从XML文件中创建Layout
	 */
	private LayoutInflater mInflater;

	/**
	 * View创建器
	 */
	private ViewCreator<E> mCreator;

	public EditTextAdapter(LayoutInflater inflater, ViewCreator<E> creator) {
		mInflater = inflater;
		mCreator = creator;
	}

	/**
	 * title : 获取当前adapter的list description :获取当前adapter的list
	 * 
	 * @param data
	 */
	public List<E> getList() {
		return mDataCache;
	}

	/**
	 * title : 更新数据集 description :更新数据集
	 * 
	 * @param data
	 */
	public void update(List<E> data) {
		mDataCache = data;

		notifyDataSetChanged();
	}

	/**
	 * title : 添加数据集 description :添加数据集
	 * 
	 * @param set
	 */
	public void add(List<E> set) {
		if (null == mDataCache)
			mDataCache = new ArrayList<E>();
		mDataCache.addAll(set);
		notifyDataSetChanged();
	}

	/**
	 * title : 添加数据元素 description :添加数据元素
	 * 
	 * @param item
	 */
	public void add(E item) {
		if (null == mDataCache)
			mDataCache = new ArrayList<E>();
		mDataCache.add(item);
		notifyDataSetChanged();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getCount()
	 */
	public int getCount() {
		return null == mDataCache ? 0 : mDataCache.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItem(int)
	 */
	public E getItem(int position) {
		return null == mDataCache ? null : mDataCache.get(position);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItemId(int)
	 */
	public long getItemId(int position) {
		return position;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#setPosition(int)
	 */
	public void setPosition(int position) {
		this.position = position;
		notifyDataSetChanged();
	}

	/*
	 * (non-Javadoc) suligen
	 * 
	 * @see android.widget.Adapter#getPosition()
	 */
	public int getPosition() {
		return position;
	}

	/*
	 * (non-Javadoc) suligen
	 * 
	 * @see android.widget.Adapter#clear()
	 */
	public void clear() {
		mDataCache.clear();

	}

	private HashMap<Integer, Boolean> isSelected;

	public HashMap<Integer, Boolean> getIsSelected() {
		return isSelected;

	}

	public void setIsSelected(HashMap<Integer, Boolean> isSelected) {
		this.isSelected = isSelected;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		holder = new ViewHolder();
		convertView = mCreator.createView(mInflater, position,
				getItem(position));
		convertView.setTag(holder);
		holder.view = convertView;
		mCreator.updateView(holder.view, position, getItem(position));
		return convertView;
	}
}